package cn.itlight.order.controller;

import cn.itlight.basic.exception.BusinessException;
import cn.itlight.basic.utils.AjaxResult;
import cn.itlight.basic.utils.LoginContext;
import cn.itlight.basic.utils.PageList;
import cn.itlight.order.domain.OrderProduct;
import cn.itlight.order.query.OrderProductQuery;
import cn.itlight.order.service.IOrderProductService;
import cn.itlight.org.domain.Employee;
import cn.itlight.org.service.IEmployeeService;
import cn.itlight.pet.domain.SearchMasterMsg;
import cn.itlight.pet.dto.HandlerMsgDto;
import cn.itlight.pet.query.SearchMasterMsgQuery;
import cn.itlight.user.domain.Logininfo;
import cn.itlight.user.domain.User;
import cn.itlight.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/orderProduct")
public class OrderProductController {
    @Autowired
    public IOrderProductService orderProductService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 保存和修改公用的
     * @param orderProduct  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody OrderProduct orderProduct){
        try {
            if( orderProduct.getId()!=null)
                orderProductService.update(orderProduct);
            else
                orderProductService.save(orderProduct);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            orderProductService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public OrderProduct get(@PathVariable("id")Long id)
    {
        return orderProductService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<OrderProduct> list(){

        return orderProductService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<OrderProduct> json(@RequestBody OrderProductQuery query)
    {
        return orderProductService.queryData(query);
    }

    @PostMapping("/shopping")
    public AjaxResult shopping(@RequestBody OrderProduct orderProduct,HttpServletRequest req){
        try {
            Logininfo logininfo = LoginContext.getLogininfo(req);
            // 1.根据LogininfoId查询用户
            User user = userService.loadByLogininfoId(logininfo.getId());
            // 设置上传用户id
            orderProduct.setUserId(user.getId());
            orderProductService.shopping(orderProduct);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }

    @PostMapping("/toOrder")
    public PageList<OrderProduct> toOrder(@RequestBody OrderProductQuery query, HttpServletRequest req){
        // 1.设置寻主消息状态为待付款，设置店铺Id
        query.setState(0L);
        Logininfo logininfo = LoginContext.getLogininfo(req);
        Employee employee = employeeService.loadByLogininfoId(logininfo.getId());
        query.setShopId(employee.getShopId());
        return orderProductService.queryData(query);
    }

    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody HandlerMsgDto handlerMsgDto){
        try {
            orderProductService.accept(handlerMsgDto);
            return AjaxResult.me();
        }catch(BusinessException e){
            e.printStackTrace();
            // 如果是自定义异常，那么返回给前端的提示语就是service层设置的提示语
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！！！");
        }
    }

    @PostMapping(value="/reject/{id}")
    public AjaxResult reject(@PathVariable("id") Long id){
        try {
            orderProductService.reject(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！"+e.getMessage());
        }
    }
}
